// component/authInput/swiper.js
const componentBehavior = require('../../js/common/ComponentBehavior')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hdimg: {
      type: Array,
      value: []
    },
    indicatorDots: {
      type: Boolean,
      value: true
    },
    vertical: {
      type: Boolean,
      value: false
    },
    circular: {
      type: Boolean,
      value: true
    },
    autoplay: {
      type: Boolean,
      value: true
    },
    /**缩略图宽 */
    thumbWidth:{
      type:Number,
      value:0
    },
    /**缩略图高 */
    thumbHeight: {
      type: Number,
      value: 0
    },
    /**是否开启oss */
    enableOss:{
      type:Boolean,
      value:false
    },
    imgheights: {
      type: Array,
      value: []
    },
    imgwidth: {
      type: Number,
      value: 750
    },
    current: {
      type: Number,
      value: 0
    },
    imgDomain:{
      type:String,
      value:''
    },
    proprietorWaterMark: {
      type: String,
      value: ''
    },
    fitHeight:{
      type:Number,
      value:0
    },
    radius:{
        type:Number,
        value:0
    },
    dots:Boolean,
    videoSrc:String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    hideVideoPlayButton: false
  },

  behaviors: [componentBehavior],

  /**
   * 组件的方法列表
   */
  methods: {
    imageLoad: function (e) {
      var fitHeight = this.data.fitHeight;
      var current = e.currentTarget.dataset.current;
      if(fitHeight>0){
        var imgheights = this.data.imgheights;
        imgheights[current] = fitHeight;
        this.setData({
          imgheights: imgheights,
        })
      }else{
        //获取图片真实宽度
        var imgwidth = e.detail.width,
          imgheight = e.detail.height,
          //宽高比
          ratio = imgwidth / imgheight;
        //计算的高度值
        var viewHeight = 750 / ratio;
        var imgheight = viewHeight;//imgwidth < 750 ? imgheight : viewHeight;
        var imgheights = this.data.imgheights;
        //把每一张图片的高度记录到数组里
        imgheights[current] = imgheight;
        this.setData({
          imgheights: imgheights
        })
      }
    },
    bindchange: function (e) {
      this.setData({ current: e.detail.current })
      let video = this.swiperVideoContent
      if (video) {
        video.pause()
      }

      this.triggerEvent("handleCurrent",e.detail.current)
    },
    /**替换链接 */
    linkTo: function (e) {
      var url = e.currentTarget.dataset.url;
      var img = e.currentTarget.dataset.img;
      var imgList = e.currentTarget.dataset.imglist;//获取data-list
      var imgSrl = [];
      for (var i = 0; i < imgList.length; i++) {
        if (this.data.proprietorWaterMark!=""){
          imgSrl[i] = this.addWaterMark(imgList[i].Src, this.data.imgDomain,this.data.proprietorWaterMark)
        }else{
          imgSrl[i] = this.data.imgDomain + imgList[i].Src;
        }
      }
      if (url == null || url == undefined || url == "") {
        //图片预览
        wx.previewImage({
          current: img, // 当前显示图片的http链接
          urls: imgSrl // 需要预览的图片http链接列表
        })
      }else{
        this.triggerEvent("linkTo", {url:url});
      }
    },
    //添加水印
    addWaterMark: function (img, imgDomain, proprietorWaterMark) {
      var src = img;
        if (proprietorWaterMark != "") {
          src = proprietorWaterMark.replace('$1', imgDomain).replace('$2', src)
        }
      return src;
    },
    /**
     * 播放视频
     */
    playVideo () {
      let that = this
      let video = this.swiperVideoContent
      if (video) {
        that.setData({
          hideVideoPlayButton: true
        })
        video.play()
        return
      }
      // 获取videoContent，播放视频
      let query = this.createSelectorQuery()
      query.select('#swiper-video').fields({
        context: true
      }).exec((res) => {
        if (!res[0]) {
          return
        }
        that.setData({
          hideVideoPlayButton: true
        })
        video = res[0].context
        video.play()
        that.swiperVideoContent = video
      })
    },
    /**
     * 视频暂停，显示播放按钮
     */
    videoPause () {
      // this.setData({
      //   hideVideoPlayButton: false
      // })
    },
    /**
     * 视频播放完成，显示播放按钮
     */
    videoEnd () {
      this.setData({
        hideVideoPlayButton: false
      })
    },
  }
})
